<?php

require_once("../../config/maptrick.conf.php");
require_once(SITE_ROOT . "lib/pChart/class/pData.class.php");
require_once(SITE_ROOT . "lib/pChart/class/pDraw.class.php");
require_once(SITE_ROOT . "lib/pChart/class/pImage.class.php");
require_once(SITE_ROOT . "class/db/DBHCountry.class.php");

class HLeagueGraph {

	private $timeseries_global;

	public function __construct() {
		$timeseries_global = array();
	}

	public function generateChart($country) {
		$data = new pData();
		
		$dbhcountry = new DBHCountry();
		$timeseries = $dbhcountry->getCountry($country->getCountryID());
		foreach ($timeseries as $timepoint) {
			$data->addPoints($timepoint->getActiveUsers() + $timepoint->getWaitingUsers(), "activewaiting");
			$data->addPoints($timepoint->getActiveUsers(), "active");
			$data->addPoints($timepoint->getDate(), "date");
			
			if (!isset($this->timeseries_global[$timepoint->getDate()])) {
				$this->timeseries_global[$timepoint->getDate()] = $this->newGlobal($timepoint->getDate());
			}
			$global = $this->timeseries_global[$timepoint->getDate()];
			$global->setActiveUsers($global->getActiveUsers() + $timepoint->getActiveUsers());
			$global->setWaitingUsers($global->getWaitingUsers() + $timepoint->getWaitingUsers());
		}		
		$data->setSerieOnAxis("activewaiting", 0);
		$data->setSerieOnAxis("active", 0);
		
		$data->setPalette("activewaiting", array("R"=>92, "G"=>171, "B"=>188, "Alpha"=>0));
		$data->setPalette("active", array("R"=>213, "G"=>193, "B"=>115, "Alpha"=>0));
		
		$data->setSerieDescription("activewaiting", "Active + waiting users");
		$data->setSerieDescription("active", "Active users");
		
		$data->setAbscissa("date");
		$data->setXAxisDisplay(AXIS_FORMAT_DATE, "m/Y");
		
		$this->renderGraph($data, $country);
	}
	
	public function generateGlobalChart() {
		$data = new pData();
		$global = $this->newGlobal(time());

		foreach ($this->timeseries_global as $timepoint) {
			$data->addPoints($timepoint->getActiveUsers() + $timepoint->getWaitingUsers(), "activewaiting");
			$data->addPoints($timepoint->getActiveUsers(), "active");
			$data->addPoints($timepoint->getDate(), "date");
		}
		$data->setSerieOnAxis("activewaiting", 0);
		$data->setSerieOnAxis("active", 0);
		
		$data->setPalette("activewaiting", array("R"=>92, "G"=>171, "B"=>188, "Alpha"=>0));
		$data->setPalette("active", array("R"=>213, "G"=>193, "B"=>115, "Alpha"=>0));
		
		$data->setSerieDescription("activewaiting", "Active + waiting users");
		$data->setSerieDescription("active", "Active users");
		
		$data->setAbscissa("date");
		$data->setXAxisDisplay(AXIS_FORMAT_DATE, "m/Y");
		
		$this->renderGraph($data, $global);
	}
	
	private function renderGraph($data, $country) {
		$image = new pImage(1000, 400, $data);
		
		$image->setGraphArea(60, 50, 980, 370);
		$image->drawRoundedFilledRectangle(7, 7, 993, 393, 5, array("R"=>240, "G"=>240, "B"=>240, "Alpha"=>100));
		$image->drawRoundedRectangle(5, 5, 995, 395, 5, array("R"=>255, "G"=>255, "B"=>255, "Alpha"=>100));
		$image->drawFilledRectangle(60, 50, 980, 370, array("R"=>255, "G"=>255, "B"=>255, "Alpha"=>100));
		
		$image->drawFromGIF(445, 12, SITE_ROOT . "images/graphs_logo.gif");
		$image->drawFromGIF(60, 22, SITE_ROOT . "flags/" . $country->getCountryID() . "_mini.gif");
		$image->setFontProperties(array("FontName"=>SITE_ROOT . "fonts/verdana.ttf", "FontSize"=>16, "R"=>75, "G"=>75, "B"=>75));
		$image->drawText(60, 18, str_replace("&amp;", "&", $country->getName()), array("Align"=>"TEXT_ALIGN_MIDDLELEFT", "Alpha"=>100));
						
		$image->setFontProperties(array("FontName"=>SITE_ROOT . "fonts/tahoma.ttf", "FontSize"=>8, "R"=>75, "G"=>75, "B"=>75));
		$image->drawLegend(860, 20, array("R"=>255, "G"=>255, "B"=>255, "Alpha"=>100));

		$image->setFontProperties(array("FontName"=>SITE_ROOT . "fonts/tahoma.ttf", "FontSize"=>8, "R"=>150, "G"=>150, "B"=>150));
		$image->drawScale(array("LabelSkip"=>91, "AxisR"=>150, "AxisG"=>150, "AxisB"=>150, "TickR"=>150, "TickG"=>150, "TickB"=>150, "Alpha"=>100));
		
		$image->drawAreaChart(array("ForceTransparency"=>100));

		$image->Render(SITE_ROOT . "graphs/" . $country->getCountryID() . "_graph_users.png");
	}

	private function newGlobal($date) {
		$global = new Country();
		$global->setCountryID(0);
		$global->setName("Global");
		$global->setEnglishName("Global");
		$global->setActiveUsers(0);
		$global->setWaitingUsers(0);
		$global->setDate($date);
		return $global;
	}

}

?>